package com.webapp.org.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.webapp.org.bean.QualifiedTestBean;
import com.webapp.org.interfaces.IQualifiedTest;
import com.webapp.org.util.condatabase.HBConfigure;

public class QualifiedTestDAO implements IQualifiedTest
{
	private QualifiedTestBean qualifiedTestBean = null;
	private SessionFactory sf = HBConfigure.getSessionFactory();
	private boolean result = false;

	@Override
	public boolean insertQualifiedTest(QualifiedTestBean qualifiedTestBean)
	{
		result = false;
		Session session = sf.openSession();
		session.beginTransaction();
		try
		{
			session.persist(qualifiedTestBean);
			session.getTransaction().commit();
			result = true;
		} catch (Exception exception)
		{
			//System.out.println(exception);
			session.getTransaction().rollback();
			System.out.println("Error occured while inserting");
		} finally
		{
			session.close();
		}
		return result;
	}

	@Override
	public boolean updateQualifiedTest(QualifiedTestBean qualifiedTestBean)
	{
		result = false;
		Session session = sf.openSession();
		session.beginTransaction();
		try
		{
			session.update(qualifiedTestBean);
			session.getTransaction().commit();
			result = true;
		} catch (Exception e)
		{
			session.getTransaction().rollback();
			System.out.println("Exception took pplace in update");
		} finally
		{
			session.close();
		}
		return result;
	}

	@Override
	public boolean deleteQualifiedTest(int qualifiedTestId)
	{
		result = false;
		Session session = sf.openSession();
		session.beginTransaction();
		try
		{
    		qualifiedTestBean = (QualifiedTestBean) session.get(
					QualifiedTestBean.class, qualifiedTestId);
			session.delete(qualifiedTestBean);
			session.getTransaction().commit();
			result = true;
		} catch (Exception e)
		{
			session.getTransaction().rollback();
			System.out.println("error while deleting");
		} finally
		{
			session.close();
		}
		return result;
	}

	@Override
	public List<QualifiedTestBean> retrieveQualifiedTest()
	{
		List<QualifiedTestBean> qualifiedTestList = null;
		Session session = sf.openSession();
		session.beginTransaction();
		try
		{
			qualifiedTestList = session.createQuery("FROM QualifiedTestBean").list();
			session.getTransaction().commit();
			result = true;
		} catch (Exception e)
		{
			session.getTransaction().rollback();
			System.out.println("Error while retriving Designation data");
		} finally
		{
			session.close();
		}
		return qualifiedTestList;
	}

	@Override
	public QualifiedTestBean getQualifiedTest(int qualifiedTestId)
	{
        Session session = sf.openSession();
        session.beginTransaction();
        
        try
        {
        	
        	qualifiedTestBean=(QualifiedTestBean)session.get
        			(QualifiedTestBean.class, qualifiedTestId);
        			
        	session.getTransaction().commit();
        }
		catch(Exception e)
		{
			session.getTransaction().rollback();
			System.out.println("Error whle fetching the bean");
		}
        finally
        {
        	session.close();
        }
		
		return qualifiedTestBean;
	}

}
